package com.zhuanzhuan.hero.user.recent.assembler;

import com.google.common.collect.Maps;
import com.zhuanzhuan.hero.social.interaction.vo.pricesheet.RecycleSheetScfVO;
import com.zhuanzhuan.hero.user.enums.RecentDataEnum;
import com.zhuanzhuan.hero.user.helper.HeroSocialHelper;
import com.zhuanzhuan.hero.user.recent.assembler.base.RecentMsgAssembleContext;
import com.zhuanzhuan.hero.user.recent.assembler.base.UserRecentMsgAssembler;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.swing.text.MaskFormatter;
import java.util.*;

/**
 * @author jiazebiao
 * @date 2024/7/11
 * @description 最近新增、更新报价单
 */
@Component
public class PriceSheetMsgAssembler implements UserRecentMsgAssembler {
    private static final String RECENT_PRICE_SHEET_SG = "%s更新了报价单";
    private static final int RECENT_DAYS = 7;

    @Resource
    private HeroSocialHelper heroSocialHelper;
    @Override
    public Map<Long, EnumMap<RecentDataEnum, String>> assemble(RecentMsgAssembleContext context) {
        Map<Long, EnumMap<RecentDataEnum, String>> rst = Maps.newHashMap();
        Date end = new Date();
        Date start = DateUtils.addDays(DateUtils.truncate(end, Calendar.DATE), -RECENT_DAYS);
        Map<Long,List<RecycleSheetScfVO>> userPriceSheetMap = heroSocialHelper.queryPriceSheet(context.getUids(), start, end);
        if(MapUtils.isEmpty(userPriceSheetMap)){
            return Maps.newHashMap();
        }
        userPriceSheetMap.forEach((uid,list)->{
            Optional<Date> latestOpt = list.stream().map(item -> item.getUpdateTime() == null ? item.getCreateTime() : item.getUpdateTime())
                    .filter(Objects::nonNull).max(Comparator.naturalOrder());
            if(!latestOpt.isPresent()){
                return;
            }
            String msg = buildRecentPriceSheetSg(latestOpt.get());
            if(StringUtils.isNotEmpty(msg)){
                EnumMap<RecentDataEnum, String> map = new EnumMap<>(RecentDataEnum.class);
                map.put(RecentDataEnum.PRICE_SHEET_UPDATE, msg);
                rst.put(uid, map);
            }
        });
        return rst;
    }

    /**
     * 根据报价单更新、新增时间组装近期动态标签信息
     * @param priceSheetTime
     * @return
     */
    private String buildRecentPriceSheetSg(Date priceSheetTime) {
        if(priceSheetTime == null){
            return Strings.EMPTY;
        }
        //日期都取整，以方便计算日期差
        Date curDate = DateUtils.truncate(new Date(), Calendar.DATE);
        priceSheetTime = DateUtils.truncate(priceSheetTime, Calendar.DATE);
        long days = (curDate.getTime() - priceSheetTime.getTime()) / (1000 * 60 * 60 * 24);
        if(days == 0L){
            return String.format(RECENT_PRICE_SHEET_SG, "今天");
        }else if(days == 1L){
            return String.format(RECENT_PRICE_SHEET_SG, "昨天");
        }else {
            return String.format(RECENT_PRICE_SHEET_SG, days + "天前");
        }
    }
}